/*     */ package com.zimbra.cs.service.util;
/*     */ 
/*     */ import com.google.common.annotations.VisibleForTesting;
/*     */ import com.google.common.collect.Lists;
/*     */ import com.zimbra.cs.mailbox.Flag;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.IndexStatus;
/*     */ import com.zimbra.cs.mailbox.MailItem.UnderlyingData;
/*     */ import com.zimbra.cs.mailbox.util.TagUtil;
/*     */ import java.io.IOException;
/*     */ import java.util.List;
/*     */ import org.json.JSONException;
/*     */ import org.json.JSONObject;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ItemData
/*     */ {
/*     */   public String sender;
/*     */   public String extra;
/*     */   public String flags;
/*     */   public String path;
/*     */   public String tags;
/*     */   public MailItem.UnderlyingData ud;
/*  35 */   private String tagsOldFmt = null;
/*     */   
/*     */   private static enum Keys {
/*  38 */     id,  uuid,  type,  parent_id,  folder_id,  index_id,  imap_id,  date,  size, 
/*  39 */     volume_id,  blob_digest,  unread,  flags,  tags,  subject,  name, 
/*  40 */     metadata,  mod_metadata,  change_date,  mod_content, 
/*  41 */     sender,  ExtraStr,  FlagStr,  Path,  TagStr,  TagNames,  Ver;
/*     */     
/*     */     private Keys() {} }
/*     */   
/*  45 */   public ItemData(MailItem mi) throws IOException { this(mi, null); }
/*     */   
/*     */   public ItemData(MailItem mi, String userData) throws IOException
/*     */   {
/*     */     try {
/*  50 */       this.sender = mi.getSender();
/*  51 */       this.extra = userData;
/*  52 */       this.flags = mi.getFlagString();
/*  53 */       this.path = mi.getPath();
/*  54 */       this.tagsOldFmt = TagUtil.getTagIdString(mi);
/*  55 */       this.tags = getTagString(mi.getTags());
/*  56 */       this.ud = mi.getUnderlyingData();
/*     */     } catch (Exception e) {
/*  58 */       throw new IOException("data error: " + e);
/*     */     }
/*     */   }
/*     */   
/*     */   public ItemData(String encoded) throws IOException {
/*     */     try {
/*  64 */       JSONObject json = new JSONObject(encoded);
/*  65 */       int version = (byte)json.getInt(Keys.Ver.toString());
/*     */       
/*  67 */       if (version > 10) {
/*  68 */         throw new IOException("unsupported data version");
/*     */       }
/*  70 */       this.ud = new MailItem.UnderlyingData();
/*  71 */       this.ud.id = json.getInt(Keys.id.toString());
/*  72 */       String uuid = json.optString(Keys.uuid.toString());
/*  73 */       if ((uuid != null) && (uuid.length() > 0)) {
/*  74 */         this.ud.uuid = uuid;
/*     */       }
/*  76 */       this.ud.type = ((byte)json.getInt(Keys.type.toString()));
/*  77 */       this.ud.parentId = json.getInt(Keys.parent_id.toString());
/*  78 */       this.ud.folderId = json.getInt(Keys.folder_id.toString());
/*     */       
/*  80 */       this.ud.indexId = MailItem.IndexStatus.NO.id();
/*  81 */       this.ud.locator = null;
/*  82 */       this.ud.imapId = json.getInt(Keys.imap_id.toString());
/*  83 */       this.ud.date = json.getInt(Keys.date.toString());
/*  84 */       this.ud.size = json.getLong(Keys.size.toString());
/*  85 */       this.ud.setBlobDigest(json.optString(Keys.blob_digest.toString()));
/*  86 */       this.ud.unreadCount = json.getInt(Keys.unread.toString());
/*  87 */       this.ud.setFlags(json.getInt(Keys.flags.toString()) | Flag.BITMASK_UNCACHED);
/*     */       
/*  89 */       this.ud.setSubject(json.optString(Keys.subject.toString()));
/*  90 */       this.ud.name = json.optString(Keys.name.toString());
/*  91 */       this.ud.metadata = json.optString(Keys.metadata.toString());
/*  92 */       this.ud.modMetadata = json.getInt(Keys.mod_metadata.toString());
/*  93 */       this.ud.dateChanged = json.getInt(Keys.change_date.toString());
/*  94 */       this.ud.modContent = json.getInt(Keys.mod_content.toString());
/*  95 */       this.sender = json.optString(Keys.sender.toString());
/*  96 */       this.extra = json.optString(Keys.ExtraStr.toString());
/*  97 */       this.flags = json.optString(Keys.FlagStr.toString());
/*  98 */       this.path = json.optString(Keys.Path.toString());
/*  99 */       getTagsFromJson(json);
/*     */     } catch (JSONException e) {
/* 101 */       throw new IOException("decode error: " + e);
/*     */     }
/*     */   }
/*     */   
/*     */   public ItemData(byte[] encoded) throws IOException {
/* 106 */     this(new String(encoded, "UTF-8"));
/*     */   }
/*     */   
/*     */   public JSONObject toJSON() throws IOException {
/*     */     try {
/* 111 */       return new JSONObject().put(Keys.id.toString(), this.ud.id).put(Keys.uuid.toString(), this.ud.uuid).put(Keys.type.toString(), this.ud.type).put(Keys.parent_id.toString(), this.ud.parentId).put(Keys.folder_id.toString(), this.ud.folderId).put(Keys.index_id.toString(), 0).put(Keys.imap_id.toString(), this.ud.imapId).put(Keys.date.toString(), this.ud.date).put(Keys.size.toString(), this.ud.size).put(Keys.volume_id.toString(), 0).putOpt(Keys.blob_digest.toString(), this.ud.getBlobDigest()).put(Keys.unread.toString(), this.ud.unreadCount).put(Keys.flags.toString(), this.ud.getFlags()).putOpt(Keys.subject.toString(), this.ud.getSubject()).putOpt(Keys.name.toString(), this.ud.name).putOpt(Keys.metadata.toString(), this.ud.metadata).put(Keys.mod_metadata.toString(), this.ud.modMetadata).put(Keys.change_date.toString(), this.ud.dateChanged).put(Keys.mod_content.toString(), this.ud.modContent).putOpt(Keys.sender.toString(), this.sender).putOpt(Keys.ExtraStr.toString(), this.extra).putOpt(Keys.FlagStr.toString(), this.flags).put(Keys.Path.toString(), this.path).putOpt(Keys.TagStr.toString(), this.tagsOldFmt).putOpt(Keys.TagNames.toString(), this.tags).put(Keys.Ver.toString(), 10);
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 142 */       throw new IOException("encode error: " + e);
/*     */     }
/*     */   }
/*     */   
/*     */   public String encode(int indent) throws IOException {
/*     */     try {
/* 148 */       return toJSON().toString(indent);
/*     */     } catch (Exception e) {
/* 150 */       throw new IOException("encode error: " + e);
/*     */     }
/*     */   }
/*     */   
/*     */   public byte[] encode() throws IOException {
/*     */     try {
/* 156 */       return toJSON().toString().getBytes("UTF-8");
/*     */     } catch (Exception e) {
/* 158 */       throw new IOException("encode error: " + e);
/*     */     }
/*     */   }
/*     */   
/*     */   @VisibleForTesting
/*     */   public static String getTagString(String[] tags) {
/* 164 */     if ((tags == null) || (tags.length == 0)) {
/* 165 */       return "";
/*     */     }
/*     */     
/*     */ 
/* 169 */     StringBuilder serialized = new StringBuilder();
/* 170 */     for (int i = 0; i < tags.length; i++) {
/* 171 */       if (i > 0) {
/* 172 */         serialized.append(':');
/*     */       }
/* 174 */       serialized.append(tags[i].replace("\\", "\\\\").replace(":", "\\:"));
/*     */     }
/* 176 */     return serialized.toString();
/*     */   }
/*     */   
/*     */   public static String[] getTagNames(String serialized)
/*     */   {
/* 181 */     if (serialized.indexOf(':') == -1) {
/* 182 */       return new String[] { serialized };
/*     */     }
/*     */     
/* 185 */     StringBuilder tag = new StringBuilder();
/* 186 */     List<String> tags = Lists.newArrayList();
/* 187 */     boolean escaped = false;
/* 188 */     int i = 0; for (int len = serialized.length(); i <= len; i++) {
/* 189 */       char c = i == len ? ':' : serialized.charAt(i);
/* 190 */       if (escaped) {
/* 191 */         tag.append(c);
/* 192 */         escaped = false;
/* 193 */       } else if (c == '\\') {
/* 194 */         escaped = true;
/* 195 */       } else if (c == ':') {
/* 196 */         if (tag.length() > 0) {
/* 197 */           tags.add(tag.toString());
/* 198 */           tag.setLength(0);
/*     */         }
/*     */       } else {
/* 201 */         tag.append(c);
/*     */       }
/*     */     }
/* 204 */     return (String[])tags.toArray(new String[tags.size()]);
/*     */   }
/*     */   
/*     */   private boolean isOldTags() {
/* 208 */     return (this.tags.length() > 0) && (Character.isDigit(this.tags.charAt(0))) && (this.tags.indexOf(":") == -1);
/*     */   }
/*     */   
/*     */   private void getTagsFromJson(JSONObject json) {
/* 212 */     this.tags = json.optString(Keys.TagNames.toString());
/* 213 */     if (this.tags.length() == 0)
/*     */     {
/* 215 */       this.tags = json.optString(Keys.TagStr.toString());
/* 216 */       if (isOldTags()) {
/* 217 */         this.tagsOldFmt = this.tags;
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public boolean tagsEqual(MailItem mi)
/*     */   {
/* 224 */     return isOldTags() ? this.tags.equals(TagUtil.getTagIdString(mi)) : this.tags.equals(getTagString(mi.getTags()));
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/util/ItemData.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */